package com.zhb.cloud.core.util;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.zhb.cloud.plant.active.model.Prize;
import com.zhb.cloud.utils.UUIDUtil;

/**
 * @ClassName: PrizeUtils
 * @Description: 奖品核心工具类
 * @author: zhb zhb1316783262@163.com
 * @date: 2017年11月17日 下午2:01:50
 *
 */
public class PrizeUtils {
	/**
	 * 根据Math.random()产生一个double型的随机数，判断每个奖品出现的概率
	 * 
	 * @param prizes
	 * @return random：奖品列表prizes中的序列（prizes中的第random个就是抽中的奖品）
	 */
	public static Prize getAwardPrize(List<Prize> prizes) {
		Prize prize = null;
		try {
			// 计算总权重
			BigDecimal sumWeight = new BigDecimal(0, MathContext.DECIMAL64);
			for (Prize p : prizes) {
				sumWeight = sumWeight.add(p.getPrizeWeight());
			}
			// 产生随机数
			BigDecimal randomNumber = new BigDecimal(Math.random(), MathContext.DECIMAL64);
			// 根据随机数在所有奖品分布的区域并确定所抽奖品
			BigDecimal decimal1 = new BigDecimal(0, MathContext.DECIMAL64);
			BigDecimal decimal2 = new BigDecimal(0, MathContext.DECIMAL64);
			for (int i = 0; i < prizes.size(); i++) {
				decimal2 = decimal2.add(prizes.get(i).getPrizeWeight().divide(sumWeight, 3, RoundingMode.HALF_UP));
				if (i == 0) {
					decimal1 = new BigDecimal(0);
				} else {
					decimal1 = decimal1
							.add(prizes.get(i - 1).getPrizeWeight().divide(sumWeight, 3, RoundingMode.HALF_UP));
				}
				if (randomNumber.compareTo(decimal1) >= 0 && randomNumber.compareTo(decimal2) <= 0) {
					prize = prizes.get(i);
					break;
				}
			}
		} catch (Exception e) {
			System.out.println("生成奖品，出错原因：" + e.getMessage());
		}
		return prize;
	}

	public static List<Prize> getPrizes() {
		List<Prize> prizes = new ArrayList<>();
		Prize prize1 = new Prize();
		prize1.setId(UUIDUtil.getId());
		prize1.setPrizeAmount(10);
		prize1.setPrizeName("iphonex");
		prize1.setPrizeWeight(new BigDecimal(0.01));
		prize1.setCreateTime(new Date());
		prize1.setUpdateTime(new Date());
		prizes.add(prize1);

		Prize prize2 = new Prize();
		prize2.setId(UUIDUtil.getId());
		prize2.setPrizeAmount(10);
		prize2.setPrizeName("vivox20");
		prize2.setPrizeWeight(new BigDecimal(0.09));
		prize2.setCreateTime(new Date());
		prize2.setUpdateTime(new Date());
		prizes.add(prize2);

		Prize prize3 = new Prize();
		prize3.setId(UUIDUtil.getId());
		prize3.setPrizeAmount(10);
		prize3.setPrizeName("华为P9");
		prize3.setPrizeWeight(new BigDecimal(0.09));
		prize3.setCreateTime(new Date());
		prize3.setUpdateTime(new Date());
		prizes.add(prize3);

		Prize prize4 = new Prize();
		prize4.setId(UUIDUtil.getId());
		prize4.setPrizeAmount(10);
		prize4.setPrizeName("蓝牙耳机");
		prize4.setPrizeWeight(new BigDecimal(0.11));
		prize4.setCreateTime(new Date());
		prize4.setUpdateTime(new Date());
		prizes.add(prize4);

		Prize prize5 = new Prize();
		prize5.setId(UUIDUtil.getId());
		prize5.setPrizeAmount(10);
		prize5.setPrizeName("苹果数据线");
		prize5.setPrizeWeight(new BigDecimal(0.1));
		prize5.setCreateTime(new Date());
		prize5.setUpdateTime(new Date());
		prizes.add(prize5);

		Prize prize6 = new Prize();
		prize6.setId(UUIDUtil.getId());
		prize6.setPrizeAmount(10);
		prize6.setPrizeName("小米充电宝");
		prize6.setPrizeWeight(new BigDecimal(0.15));
		prize6.setCreateTime(new Date());
		prize6.setUpdateTime(new Date());
		prizes.add(prize6);

		Prize prize7 = new Prize();
		prize7.setId(UUIDUtil.getId());
		prize7.setPrizeAmount(10);
		prize7.setPrizeName("iphonex");
		prize7.setPrizeWeight(new BigDecimal(0.05));
		prize7.setCreateTime(new Date());
		prize7.setUpdateTime(new Date());
		prizes.add(prize7);

		Prize prize8 = new Prize();
		prize8.setId(UUIDUtil.getId());
		prize8.setPrizeAmount(10);
		prize8.setPrizeName("2元话费券");
		prize8.setPrizeWeight(new BigDecimal(0.15));
		prize8.setCreateTime(new Date());
		prize8.setUpdateTime(new Date());
		prizes.add(prize8);

		Prize prize9 = new Prize();
		prize9.setId(UUIDUtil.getId());
		prize9.setPrizeAmount(10);
		prize9.setPrizeName("2元流量券");
		prize9.setPrizeWeight(new BigDecimal(0.25));
		prize9.setCreateTime(new Date());
		prize9.setUpdateTime(new Date());
		prizes.add(prize9);

		return prizes;

	}
}
